package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.Admin;
import com.tgu.home_action.pojo.Memo;
import com.tgu.home_action.pojo.ResponseData;
import com.tgu.home_action.pojo.Volunteer;
import com.tgu.home_action.service.MemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "备忘录管理")
@RestController
@RequestMapping("/memo")
public class MemoController {
    @Autowired
    private MemoService memoService;

    @ApiOperation("志愿者添加备忘录")
    @PreAuthorize("hasRole('volunteer')")
    @PostMapping("/volunteer")
    public ResponseData<Object> volunteerAddMemo(@Validated(Memo.insert.class)@RequestBody Memo memo, @ApiIgnore @ModelAttribute("volunteerUser")Volunteer volunteer){
        return memoService.volunteerAddMemo(memo,volunteer);
    }

    @ApiOperation("管理员添加备忘录")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    @PostMapping("/admin")
    public ResponseData<Object> adminAddMemo(@Validated(Memo.insert.class)@RequestBody Memo memo, @ApiIgnore @ModelAttribute("adminUser") Admin admin){
        System.out.println(memo);
        return memoService.adminAddMemo(memo,admin);
    }

    @ApiOperation("查询备忘录")
    @GetMapping
    public ResponseData<List<Memo>> getMemo(@RequestBody Memo memo){
        return memoService.getMemo(memo);
    }

    @ApiOperation("志愿者修改备忘录")
    @PreAuthorize("hasRole('volunteer')")
    @PutMapping("/volunteer")
    public ResponseData<Object> volunteerUpdateMemo(@Validated(Memo.update.class)@RequestBody Memo memo,@ApiIgnore @ModelAttribute("volunteerUser")Volunteer volunteer){
        return memoService.volunteerUpdateMemo(memo,volunteer);
    }

    @ApiOperation("管理员修改备忘录")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    @PutMapping("/admin")
    public ResponseData<Object> adminUpdateMemo(@Validated(Memo.update.class)@RequestBody Memo memo){
        return memoService.adminUpdateMemo(memo);
    }

    @ApiOperation("志愿者删除备忘录")
    @PreAuthorize("hasRole('volunteer')")
    @DeleteMapping("/volunteer")
    public ResponseData<Object> volunteerDeleteMemo(@RequestParam Integer memoId,@ApiIgnore @ModelAttribute("volunteerUser")Volunteer volunteer){
        return memoService.volunteerDeleteMemo(memoId,volunteer);
    }


    @ApiOperation("管理员删除备忘录")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    @DeleteMapping("/admin")
    public ResponseData<Object> volunteerUpdateMemo(@RequestParam Integer id){
        return memoService.adminDeleteMemo(id);
    }
}
